package com.hevs.adibilis;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {


	private static JSONArray nodes;
	private static HashMap<String, String> map;
	private static ArrayList<HashMap<String, String>> nodesList;

	public static ArrayList<HashMap<String, String>> parseFromJSON(JSONArray array, String[] fields) throws JSONException{

		JSONParser.clean();
		
		nodesList = new ArrayList<HashMap<String, String>>();
		
		// Getting Array of Contacts
		nodes = array;

		// looping through All Contacts
		for(int i = 0; i < nodes.length(); i++){
			JSONObject c = nodes.getJSONObject(i);

			
			// creating new HashMap
			map = new HashMap<String, String>();
			
			for (String field : fields) {
				
				// Storing each json item in variable
				String value = c.getString(field);
				map.put(field, c.getString(field)); // adding each child node to HashMap key => value
				Log.d("TAG", "-Node : " + field + ", " + value);
				
			}
			
			// adding HashList to ArrayList
			nodesList.add(map); //
			

		}
		return nodesList;

	}
	
	private static void clean(){
		
		nodes = null;
		nodesList = null;
		map = null;
		
	}
	
}
